package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IUserCommemorateService;
import com.mdd.admin.validate.UserCommemorateCreateValidate;
import com.mdd.admin.validate.UserCommemorateUpdateValidate;
import com.mdd.admin.validate.UserCommemorateSearchValidate;
import com.mdd.admin.vo.UserCommemorateListedVo;
import com.mdd.admin.vo.UserCommemorateDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.UserCommemorate;
import com.mdd.common.mapper.UserCommemorateMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 纪念日提醒实现类
 * @author DEREK
 */
@Service
public class UserCommemorateServiceImpl implements IUserCommemorateService {
        
    @Resource
    UserCommemorateMapper userCommemorateMapper;

    /**
     * 纪念日提醒列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<UserCommemorateListedVo>
     */
    @Override
    public PageResult<UserCommemorateListedVo> list(PageValidate pageValidate, UserCommemorateSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<UserCommemorate> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        userCommemorateMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:uid:int",
            "=:userId@user_id:int",
            "like:name:str",
            "=:time:str",
            "=:isRemind@is_remind:int",
            "=:extend:str",
        });

        IPage<UserCommemorate> iPage = userCommemorateMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<UserCommemorateListedVo> list = new LinkedList<>();
        for(UserCommemorate item : iPage.getRecords()) {
            UserCommemorateListedVo vo = new UserCommemorateListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 纪念日提醒详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return UserCommemorate
     */
    @Override
    public UserCommemorateDetailVo detail(Integer id) {
        UserCommemorate model = userCommemorateMapper.selectOne(
                new QueryWrapper<UserCommemorate>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        UserCommemorateDetailVo vo = new UserCommemorateDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 纪念日提醒新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(UserCommemorateCreateValidate createValidate) {
        UserCommemorate model = new UserCommemorate();
        model.setUid(createValidate.getUid());
        model.setUserId(createValidate.getUserId());
        model.setName(createValidate.getName());
        model.setTime(createValidate.getTime());
        model.setIsRemind(createValidate.getIsRemind());
        model.setExtend(createValidate.getExtend());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        userCommemorateMapper.insert(model);
    }

    /**
     * 纪念日提醒编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(UserCommemorateUpdateValidate updateValidate) {
        UserCommemorate model = userCommemorateMapper.selectOne(
                new QueryWrapper<UserCommemorate>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUid(updateValidate.getUid());
        model.setUserId(updateValidate.getUserId());
        model.setName(updateValidate.getName());
        model.setTime(updateValidate.getTime());
        model.setIsRemind(updateValidate.getIsRemind());
        model.setExtend(updateValidate.getExtend());
        userCommemorateMapper.updateById(model);
    }

    /**
     * 纪念日提醒删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        UserCommemorate model = userCommemorateMapper.selectOne(
                new QueryWrapper<UserCommemorate>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        userCommemorateMapper.delete(new QueryWrapper<UserCommemorate>().eq("id", id));
    }

}
